package com.intelligent.ispc.common.persistence.jdbc;

import com.intelligent.ispc.common.utils.Collections3;

import java.util.Collection;
import java.util.Map;

/**
 * Created by Peter on 15/11/25.
 */
public class JdbcDynamicFiltersUtil {

    private final StringBuffer queryPageSql;
    private StringBuffer orderBySql;
    private String whereSql;

    private JdbcDynamicFiltersUtil(String queryPageSql) {
        this.queryPageSql = new StringBuffer(queryPageSql);
    }

    /**
     * Instance.
     *
     * @param sql
     * @return
     */
    public static JdbcDynamicFiltersUtil newInstance(String sql) {
        return new JdbcDynamicFiltersUtil(sql);
    }

    /**
     * Build the searching params.
     *
     * @param searchParams
     * @return
     */
    public JdbcDynamicFiltersUtil buildParams(Map searchParams) {
        Collection<JdbcSeachFilter> filters = JdbcSeachFilter.parse(searchParams).values();
        StringBuffer resultBuffer = new StringBuffer();

        if (Collections3.isNotEmpty(filters)) {
            for (JdbcSeachFilter filter : filters) {
                // logic operator
                switch (filter.operator) {
                    case EQ:
                        resultBuffer.append(filter.fieldName).append("='").append(filter.value).append("'");
                        break;
                    case LIKE:
                        resultBuffer.append(filter.fieldName).append(" LIKE '%").append(filter.value).append("%'");
                        break;
                    case GT:
                        resultBuffer.append(filter.fieldName).append(">").append(filter.value);
                        break;
                    case LT:
                        resultBuffer.append(filter.fieldName).append("<").append(filter.value);
                        break;
                    case GTE:
                        resultBuffer.append(filter.fieldName).append(">=").append(filter.value);
                        break;
                    case LTE:
                        resultBuffer.append(filter.fieldName).append("<=").append(filter.value);
                        break;
                }
            }
        }
        this.whereSql = resultBuffer.toString();

        return this;
    }

    /**
     * Add the order condition with chain.
     *
     * @param byName
     * @param order
     * @return
     */
    public JdbcDynamicFiltersUtil addSort(String byName, String order) {
        StringBuffer softSb = null;

        if (orderBySql == null || "".equals(orderBySql)) {
            softSb = new StringBuffer(" ORDER BY ");
            softSb.append(byName).append(" ").append(order);
            this.orderBySql = softSb;

            return this;
        }

        this.orderBySql.append(",").append(byName).append(" ").append(order);

        return this;
    }

    /**
     * Add order field and will cover the exist one.
     *
     * @param orderBy
     * @return
     */
    public JdbcDynamicFiltersUtil addSoft(String orderBy) {
        StringBuffer softSb = new StringBuffer(" ORDER BY ");
        this.orderBySql = softSb.append(orderBy);

        return this;
    }

    /**
     * Build the sql.
     *
     * @return
     */
    public String builder() {
        if (whereSql != null && !"".equals(whereSql)) {
            queryPageSql.append(" WHERE ").append(whereSql);
        }

        if (orderBySql != null && !"".equals(orderBySql)) {
            queryPageSql.append(orderBySql);
        }

        return queryPageSql.toString();
    }
}
